package com.rz.htk.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.template.entity.Template;
import com.rz.htk.template.entity.vo.TemplateDeleteParam;
import com.rz.htk.template.mapper.TemplateMapper;
import com.rz.htk.template.service.ITemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName TemplateServiceImpl
 * @Description 模板service实现类
 * @Author maweiyue
 * @Date 2019/4/17 20:37
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<TemplateMapper, Template> implements ITemplateService {

    @Override
    public List<Template> listAll() {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        return list(queryWrapper);
    }

    @Override
    public List<Template> listByType(String type) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("del_flag",0);
        queryWrapper.eq("type",type);
        return list(queryWrapper);
    }

    @Override
    public IPage<Template> getPage(Template Template) {
        QueryWrapper<Template> wrapper = new QueryWrapper();
        if (StringUtils.isNotEmpty(Template.getName())) {
            wrapper.like("name", Template.getName());
        }
        if (StringUtils.isNotEmpty(Template.getType())) {
            wrapper.eq("type", Template.getType());
        }
        wrapper.eq("del_flag",0);
        Page<Template> page = new Page<>(Template.getPageNo(), Template.getPageSize());
        IPage<Template> desktopIPage = page(page, wrapper);
        buildResult(desktopIPage.getRecords());
        return desktopIPage;
    }

    private void buildResult(List<Template> records) {
        records.forEach(item->{

        });
    }

    @Override
    @Transactional
    public boolean saveTemplate(Template Template){
        if (Template.getId() == null) {
            if (CollectionUtils.isNotEmpty(listTemplateByName(Template.getName()))) {
                throw new GlobalException("模板名称【" + Template.getName() + "】已存在！");
            }
            Template.setDelFlag(0);
            Template.setCrteTime(LocalDateTime.now());
        } else {
            List<Template> oldTemplateList = listTemplateByName(Template.getName());
            if (CollectionUtils.isNotEmpty(oldTemplateList) && !Template.getId().equals(oldTemplateList.get(0).getId())) {
                throw new GlobalException("模板名称【" + Template.getName() + "】已存在！");
            }
        }
        boolean isOk = this.saveOrUpdate(Template);
        if (!isOk) {
            throw new GlobalException("模板信息保存失败！");
        }
        return isOk;
    }

    @Override
    @Transactional
    public boolean batchDelete(TemplateDeleteParam prdDeleteParam){
        prdDeleteParam.getIds().forEach(id -> {
            Template Template = this.getById(id);
            if (Template != null) {
                Template.setDelFlag(1);
                boolean isOk = this.saveOrUpdate(Template);
                if (!isOk) {
                    throw new GlobalException("模板信息【" + Template.getName() + "】删除失败！");
                }
            } else {
                throw new GlobalException("模板信息不存在，删除失败！");
            }
        });
        return true;
    }

    /**
     * 根据模板名称获取模板列表
     * @param prdName
     * @return
     */
    @Override
    public List<Template> listTemplateByName(String prdName) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", prdName);
        queryWrapper.eq("del_flag", 0);
        return list(queryWrapper);
    }
}
